<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Http\Repositories\DialRecord;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Models\Business\BusinessList;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class BusinessDialRecordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DialRecord(), function (Grid $grid) {
            $grid->model()->where('type','shid');
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('uid',__('member.info'))->display(function ($value) {
                $memberInfo = (new MemberList())->getDetails($value);
                $data = "<div style='display: flex'>";
                $avatar = $memberInfo->avatar ?? '/static/images/default.png';
                $data .="<img src='".$avatar."' data-action='preview-img' class='img img-thumbnail' style='width: 70px;height: 7S0px;'>";
                $username = $memberInfo->username ?? '未设置';
                $mobile = $memberInfo->mobile ?? '未绑定';
                $email = $memberInfo->email ?? '未绑定';
                $data .="<div style='margin-left: 10px;'>";
                    $data .="<div style='font-weight: bold;font-size: 16px;'>".$username."</div>";
                    $data .="<div><i style='font-size: 12px;' class='feather icon-phone'></i> ".$mobile."</div>";
                    $data .="<div><i style='font-size: 12px;' class='feather icon-mail'></i> ".$email."</div>";
                $data .="</div>";
                $data .="</div>";
                return $data;
            });
            $grid->column('info_id',__('global.info'))->display(function ($value) {
                $businessInfo = (new BusinessList())->getDetails($value);
                $data  ="<div style='display: flex;flex-direction: column;'>";
                if ($businessInfo){
                    $name = $businessInfo->name ?? '未设置';
                    $tel = $businessInfo->tel ?? '未设置';
                    $data .="<div style='font-weight: bold;font-size: 16px;'>{$name}</div>";
                    $data .="<div>ID: $value</div>";
                    $data .="<div><i style='font-size: 12px;' class='feather icon-phone'></i> {$tel}</div>";
                    $data .="</div>";
                }
                $data .="</div>";

                return $data;
            });
            $grid->column('type')->width(100)->display(function ($value) {
                if ($value == 'shid'){
                    return '商家';
                }
            });
            $grid->column('price')->width(100)->prepend('￥');
            $grid->column('note')->width(100);
            $grid->column('created_at')->width(100);
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableViewButton();
            $grid->quickSearch('id', 'uid', 'info_id','');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DialRecord(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('info_id');
            $show->field('type');
            $show->field('price');
            $show->field('note');
            $show->field('crts');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new DialRecord(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('info_id');
            $form->text('type');
            $form->text('price');
            $form->text('note');
            $form->text('crts');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
